home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2005 December / DPPCPRO1205.ISO / Essentials / Programming / Basic4GL / Setup Basic4GL v2.3.1.exe / $INSTDIR / Programs / Flyaround2.gb < prev    next >
Encoding:
Text File  |  2005-07-29  |  6.3 KB  |  163 lines

  1. const gridSize = 100, viewRange = 14, space# = 20
  2. const turnSpeed# = 3, bankSpeed# = 1, accell# = 0.1
  3. dim h#(gridSize)(gridSize), th#(gridSize)(gridSize)
  4. dim light#(gridSize)(gridSize)
  5.  
  6. struc SPlayer
  7.     dim pos#(3)(3), speed#
  8. endstruc
  9.  
  10. dim SPlayer player
  11.  
  12. dim x, y, cx, cy, t1#(3), t2#(3), t3#(3), t4#(3), &temp#(), tx, ty, i, i2
  13. dim turnx#, turny#, sum#, norm#(3), lightSource#(3), col#(2), tl#(3)
  14. dim tex, tex2, water, min#, max#, height#, wave#, a$
  15.  
  16. lightSource# = Normalize (vec4 (0, 2, 1, 1))
  17.  
  18. for i2 = 1 to 10
  19.     printr i2
  20.     for i = 1 to gridSize * gridSize * i2 / 2
  21.         sum# = (rnd()%1000)/40 - 5
  22.         sum# = sum# * (11 - i2)
  23.         x = rnd()%gridSize: y = rnd()%gridSize
  24.         h# (x)(y) = h#(x)(y) + sum#
  25.     next
  26.     
  27.     th# = h#
  28.     for x = 0 to gridSize
  29.         for y = 0 to gridSize
  30.             sum# = 0
  31.             for cx = x - 1 to x + 1
  32.                 for cy = y - 1 to y + 1
  33.                     sum# = sum# + th#(cx %gridSize)(cy %gridSize)
  34.                 next
  35.             next
  36.             h#(x)(y) = sum# / 9
  37.         next
  38.     next
  39. next
  40.  
  41. for y = 0 to gridSize
  42.     for x = 0 to gridSize
  43.         &temp# = &t1#: ty = y - 1: tx = x - 1: gosub MakeVec
  44.         &temp# = &t2#: ty = y + 1: tx = x + 1: gosub MakeVec
  45.         &temp# = &t3#: ty = y - 1: tx = x + 1: gosub MakeVec
  46.         &temp# = &t4#: ty = y + 1: tx = x - 1: gosub MakeVec
  47.         norm# = Normalize (CrossProduct (t2# - t1#, t4# - t3#))
  48.         light# (x)(y) = -(norm# * lightSource#)
  49.         light# (x)(y) = light# (x)(y) * light# (x)(y) * light# (x)(y) + .2
  50.         if light#(x)(y) < .2 then light#(x)(y) = .2 endif
  51.     next
  52. next
  53.  
  54. glEnable (GL_FOG)
  55. glFogf (GL_FOG_END, viewRange * space# * 2)
  56.  
  57. glFogf (GL_FOG_MODE, GL_LINEAR)
  58. tex = LoadMipMapTexture ("textures\00009.jpg")
  59. tex2 = LoadMipMapTexture ("textures\water.bmp")
  60. glEnable (GL_TEXTURE_2D)
  61. glEnable (GL_CULL_FACE)
  62.  
  63. water = h#(0)(0) + 10
  64. player.pos# = MatrixTranslate (0, water+5, 0) * MatrixRotateX (0)
  65. player.speed# = 1
  66.  
  67.  
  68.  
  69. while true
  70.     gosub Render
  71.     while SyncTimer (30)
  72.         gosub Update
  73.     wend
  74. wend
  75.  
  76. end
  77.  
  78. Render:
  79.     glClear (GL_DEPTH_BUFFER_BIT or GL_COLOR_BUFFER_BIT)
  80.     
  81.     ' Camera
  82.     glLoadMatrixf (RTInvert (player.pos#))
  83.     t1# = player.pos#(3) - player.pos#(2) * viewRange * space#
  84.     cx = t1#(0) / space#
  85.     cy = t1#(2) / space#
  86.     for y = cy - viewRange to cy + viewRange
  87.         for x = cx - viewRange to cx + viewRange
  88.             max# = -10000
  89.             min# = 10000
  90.             &temp# = &t1#: tx = x:   ty = y:   gosub MakeVec: tl#(0) = light#(tx %gridSize)(ty %gridSize)
  91.             &temp# = &t2#: tx = x+1: ty = y:   gosub MakeVec: tl#(1) = light#(tx %gridSize)(ty %gridSize)
  92.             &temp# = &t3#: tx = x+1: ty = y+1: gosub MakeVec: tl#(2) = light#(tx %gridSize)(ty %gridSize)
  93.             &temp# = &t4#: tx = x:   ty = y+1: gosub MakeVec: tl#(3) = light#(tx %gridSize)(ty %gridSize)
  94.             if (player.pos#(3)(1) + 2 >= water and max# > water) or (player.pos#(3)(1) - 2 < water and min# < water) then
  95.                 if player.pos#(3)(1) < water then   col# = vec3 (.3, .3, 1)
  96.                 else                                col# = vec3 (1, 1, 1)
  97.                 endif
  98.                 glBindTexture (GL_TEXTURE_2D, tex)
  99.                 glBegin (GL_TRIANGLE_FAN)
  100.                     glTexCoord2f (0, 1): glColor3fv (col# * tl#(3)): glVertex3fv (t4#)
  101.                     glTexCoord2f (1, 1): glColor3fv (col# * tl#(2)): glVertex3fv (t3#)
  102.                     glTexCoord2f (1, 0): glColor3fv (col# * tl#(1)): glVertex3fv (t2#)
  103.                     glTexCoord2f (0, 0): glColor3fv (col# * tl#(0)): glVertex3fv (t1#)
  104.                 glEnd ()
  105.             endif
  106.             
  107.             if min# < water then
  108.                 t1#(1) = water + sind((x)  *40+(y)  *13+wave#*2)*1 + sind((x)  *13-(y)  *40+wave#*1.7)*1
  109.                 t2#(1) = water + sind((x+1)*40+(y)  *13+wave#*2)*1 + sind((x+1)*13-(y)  *40+wave#*1.7)*1
  110.                 t3#(1) = water + sind((x+1)*40+(y+1)*13+wave#*2)*1 + sind((x+1)*13-(y+1)*40+wave#*1.7)*1
  111.                 t4#(1) = water + sind((x)  *40+(y+1)*13+wave#*2)*1 + sind((x)  *13-(y+1)*40+wave#*1.7)*1
  112. '                glEnable (GL_BLEND)
  113. '                glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
  114.                 glDisable (GL_CULL_FACE)
  115.                 glColor3f (1, 1, 1)
  116.                 glBindTexture (GL_TEXTURE_2D, tex2)
  117.                 glBegin (GL_TRIANGLE_FAN)
  118.                     glTexCoord2f (0, 1): glVertex3fv (t4#)
  119.                     glTexCoord2f (1, 1): glVertex3fv (t3#)
  120.                     glTexCoord2f (1, 0): glVertex3fv (t2#)
  121.                     glTexCoord2f (0, 0): glVertex3fv (t1#)
  122.                 glEnd ()
  123. '                glDisable (GL_BLEND)
  124.                 glEnable (GL_CULL_FACE)
  125.             endif
  126.         next
  127.     next
  128.     
  129.     SwapBuffers ()
  130.     
  131.  
  132.     return
  133.     
  134. makevec:
  135.     height# = h#(tx %gridSize)(ty %gridSize)
  136.     if height# < min# then min# = height# endif
  137.     if height# > max# then max# = height# endif    
  138.     temp# = vec4 (tx * space#, height#, ty * space#, 1)
  139.     return
  140.  
  141. update:
  142.     wave# = wave# + 1
  143.     turnx# = -Mouse_XD() * turnSpeed# * 5: turny# = -Mouse_YD() * turnSpeed# * 5
  144.     player.speed# = player.speed# + Mouse_Wheel () * accell#
  145.     if ScanKeyDown (VK_LEFT) then turnx# = turnx# + turnSpeed# endif
  146.     if ScanKeyDown (VK_RIGHT)then turnx# = turnx# - turnSpeed# endif
  147.     if ScanKeyDown (VK_UP)   then turny# = turny# + turnSpeed# endif
  148.     if ScanKeyDown (VK_DOWN) then turny# = turny# - turnSpeed# endif
  149.     if ScanKeyDown (Asc ("A")) then player.speed# = player.speed# + accell# endif
  150.     if ScanKeyDown (Asc ("Z")) then player.speed# = player.speed# - accell# endif
  151.     if KeyDown ("Q") or KeyDown ("q") then player.pos#(3) = player.pos#(3) + vec4 (0,1,0,1) endif
  152.     if ScanKeyDown (VK_SPACE) then  player.speed# = 0 endif
  153.     a$ = Inkey$ ()
  154.     if a$ = "N" or a$ = "n" then water = water + 1 endif
  155.     if a$ = "M" or a$ = "m" then water = water - 1 endif
  156.     
  157.     player.pos# = player.pos# * MatrixRotateZ(turnx#) * MatrixRotateX(-turny#)
  158.     t1# = player.pos#(3)
  159.     player.pos# = MatrixRotateY (player.pos#(0)(1)*bankSpeed#) * player.pos# 
  160.     player.pos#(3) = t1#
  161.     player.pos# = Orthonormalize (player.pos#)
  162.     player.pos#(3) = player.pos#(3) - player.pos#(2) * player.speed#
  163.     return